//
//  SocialTagSearchView.m
//  ShowYork
//
//  Created by hxh on 2020/12/21.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "DynamicTopicSearchView.h"

@interface DynamicTopicSearchView ()<UITextFieldDelegate>
@property (nonatomic,strong)UIImageView *tagImgView;
@property (nonatomic,strong)UIView *searchView;
@property (nonatomic,strong)UILabel *cancelLb;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UITextField *searchTF;
@property (nonatomic,strong)UIButton *deleteButton;
@property (nonatomic,strong)UIImageView *deleteImgView;
@end

@implementation DynamicTopicSearchView



- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    self.searchView = [[UIView alloc]init];
    self.searchView.backgroundColor = [UIColor whiteColor];
    self.searchView.layer.cornerRadius = 15;
    self.searchView.layer.masksToBounds = YES;
    [self addSubview:self.searchView];
    
    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn addTarget:self action:@selector(cancelBtnMethod) forControlEvents:UIControlEventTouchUpInside];;
    [self addSubview:self.cancelBtn];
    
    
    self.cancelLb = [[UILabel alloc]init];
    self.cancelLb.text = @"取消";
    self.cancelLb.textColor = C51;
    self.cancelLb.font = font(14);
    [self addSubview:self.cancelLb];
    
    self.tagImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"social_#_add_black_tag"]];
    [self addSubview:self.tagImgView];
    
    self.searchTF = [[UITextField alloc]init];
    self.searchTF.textColor = C51;
    self.searchTF.font = font(14);
    self.searchTF.placeholder = @"话题";
    self.searchTF.returnKeyType = UIReturnKeySearch;
    self.searchTF.delegate = self;
    self.searchTF.enablesReturnKeyAutomatically = YES;
    [self.searchTF addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:self.searchTF];
    
    self.deleteImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_black_clear_tag"]];
    self.deleteImgView.hidden = YES;
    [self addSubview:self.deleteImgView];
    
    self.deleteButton = [[UIButton alloc]init];
    [self.deleteButton addTarget:self action:@selector(deleteMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.deleteButton];
  
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.centerY.equalTo(self);
        make.right.equalTo(self.cancelLb.mas_left).offset(-9);
        make.left.equalTo(self).offset(16);
    }];
    [self.cancelLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-16);
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self);
        make.right.equalTo(self);
        make.left.equalTo(self.cancelLb);
    }];

    [self.tagImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self.searchView.mas_left).offset(9);
        make.size.mas_equalTo(CGSizeMake(14, 14));
    }];
    [self.deleteImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self.searchView.mas_right).offset(-8);
    }];
    [self.searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(20);
        make.left.equalTo(self.tagImgView.mas_right).offset(8);
        make.right.equalTo(self.deleteImgView.mas_left).offset(-8);
        make.centerY.equalTo(self.searchView);
    }];

    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self.searchView);
        make.width.equalTo(@30);
    }];
    
    Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel = object_getIvar(self.searchTF, ivar);
    placeholderLabel.textColor = RGBA(204, 204, 204, 1);
    
}

- (void)setPlaceholderLabel:(NSString *)placeholderLabel{
    _placeholderLabel = placeholderLabel;
    if (placeholderLabel.length != 0) {
        self.searchTF.placeholder = placeholderLabel;
    }
}
- (void)setBecomeFirstResponder:(BOOL)becomeFirstResponder{
    _becomeFirstResponder = becomeFirstResponder;
    if (becomeFirstResponder) {
        [self.searchTF becomeFirstResponder];
    }else{
        [self.searchTF resignFirstResponder];
    }
}

- (void)cancelBtnMethod{
    
    [self.searchTF resignFirstResponder];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(removeCurrentViewController)]) {
        [self.delegate removeCurrentViewController];
    }
}
- (void)deleteMethod{
    self.deleteButton.hidden = YES;
    self.deleteImgView.hidden = YES;
    self.searchTF.text = @"";
    if (self.delegate && [self.delegate respondsToSelector:@selector(textSearch:)]) {
        [self.delegate textChange:@""];
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textSearch:)]) {
        [self.delegate textSearch:textField.text];
    }
    return YES;
}
- (void)textDidChange:(UITextField *)textField{
    self.deleteImgView.hidden = textField.text.length == 0;
    self.deleteButton.hidden = textField.text.length == 0;
    if (self.delegate && [self.delegate respondsToSelector:@selector(textChange:)]) {
        [self.delegate textChange:textField.text];
    }
}
@end
